<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<button onclick="WebSocketTest()">连接WebSocket</button>
<button onclick="sendHeart()">发送心跳检测</button>
<script>

    var ws;

    // 用法
    function sleepAndSendHeart() {
        console.log("发送心跳");
        // 这里写sleep之后需要去做的事情
        ws.send("{\"action\":2,\"token\":\"b3256301-56f3-4e42-859d-9a55664afb96\"}");
    }


    function sendHeart() {

        console.log("执行心跳发送");
        // setTimeout("sleepAndSendHeart()", 3000);

        for (var i = 0; i < 36000; i++) {
            (function (i) {
                setTimeout(function () {
                    sleepAndSendHeart();
                }, (i + 1) * 10000);
            })(i)
        }

    }

    function WebSocketTest() {

        if ("WebSocket" in window) {

            ws = new WebSocket("ws://localhost:30001/ws");
            ws.onopen = function () {
                // ws.send("哇哈哈");
                ws.send("{\"action\":1,\"jsonObjStr\":\"{\\\"area\\\":\\\"330206\\\",\\\"device\\\":\\\"IOS\\\",\\\"orgCode\\\":\\\"30\\\",\\\"userId\\\":\\\"1\\\"}\",\"token\":\"b3256301-56f3-4e42-859d-9a55664afb96\"}");
                /*
                action 动作
                jsonObjStr 上送数据，json形式的字符串
                token 连接认证
                 */
            };
            ws.onmessage = function (evt) {
                var received_msg = evt.data;
                console.log(evt)
            };


        }
    }
</script>
</body>
</html>